<?php
/**
 * Created by PhpStorm.
 * User: guokunsheng
 * Date: 2018/7/25
 * Time: 17:01
 */

namespace app\everyday\controller;

use think\Controller;

//use think\Db;

/**
 * 每日运势 ---  凶
 * Class Luckdisasters
 * @package app\everyday\controller
 */
class Luckdisasters extends Controller
{
    // 行运数据
    public $transitsData = [];
    // 三限数据
    public $thirdprogressedData = [];

    public function init()
    {
        $money = $this->getMoneyScore();
        $work = $this->getWorkScore();
        $health = $this->getHealthScore();
        $study = $this->getStudyScore();
        $love = $this->getLoveScore();
        $talk = $this->getTalkScore();
        $travel = $this->getTravelScore();
        $family = $this->getFamilyScore();

        $data = [
            'money' => $money,
            'work' => $work,
            'health' => $health,
            'study' => $study,
            'love' => $love,
            'talk' => $talk,
            'travel' => $travel,
            'family' => $family,
        ];

        return $data;
    }

    public function initForYear()
    {
        $money = $this->getMoneyScore();
        $work = $this->getWorkScore();
        $love = $this->getLoveScore();
        // 20190311 年运曲线新增学业模块
        $study = $this->getStudyScore();

        $modulus = 10;
        // 保留整数
        $money = round(($money['house']['good'] + $money['planet']['good'] - $money['house']['bad'] - $money['planet']['bad']) * $modulus);
        $work = round(($work['house']['good'] + $work['planet']['good'] - $work['house']['bad'] - $work['planet']['bad']) * $modulus);
        $love = round(($love['house']['good'] + $love['planet']['good'] - $love['house']['bad'] - $love['planet']['bad']) * $modulus);
        $study = round(($study['house']['good'] + $study['planet']['good'] - $study['house']['bad'] - $study['planet']['bad']) * $modulus);

        $data = [
            0 => 0,
            1 => $love,
            2 => $money,
            3 => $work,
            4 => $study,
        ];

        return $data;
    }

    protected function getMoneyScore()
    {
        $house = [2, 8];
        $deputation = [3];
        $luck = [3, 5];
        $disasters = [4, 6, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);

        return $score;
    }

    protected function getWorkScore()
    {
        $house = [10, 6];
        $deputation = [0];
        $luck = [3, 5];
        $disasters = [4, 6, 7, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    protected function getHealthScore()
    {
        $house = [6, 8, 1];
        $deputation = [];
        $luck = [3, 5];
        $disasters = [4, 6, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    protected function getStudyScore()
    {
        $house = [3, 9];
        $deputation = [2];
        $luck = [3, 5];
        $disasters = [4, 6, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    /**
     * 获取婚恋吉凶
     * @return array
     */
    protected function getLoveScore()
    {
        $house = [5, 7];
        $deputation = [3];
        $luck = [3, 5];
        $disasters = [6, 7, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    protected function getTalkScore()
    {
        $house = [7, 11];
        $deputation = [3, 2];
        $luck = [3, 5];
        $disasters = [4, 6, 7, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    protected function getTravelScore()
    {
        $house = [3, 9];
        $deputation = [2];
        $luck = [3, 5];
        $disasters = [4, 6, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    protected function getFamilyScore()
    {
        $house = [4];
        $deputation = [1];
        $luck = [3, 5];
        $disasters = [4, 6, 7, 8, 9];
        $house1_protected = config('astrolog.protect_ancient_id')[$this->transitsData[0]['houses'][1]['in_sign_id'] - 1];
        $main = [0, 1, $house1_protected];

        $score = $this->calculate($house, $deputation, $luck, $disasters, $main);
//        print_r($score);

        return $score;
    }

    /**
     * 计算得分
     * @param array $house 宫位
     * @param array $deputation 征象星
     * @param array $luck 吉星
     * @param array $disasters 凶星
     * @param array $main 主星
     * @return array
     */
    private function calculate(array $house, array $deputation, array $luck, array $disasters, array $main)
    {
        $score = [
            'house' => [],
            'deputation' => []
        ];
        $na_planets = $this->transitsData[0]['planets'];// 行运-本命星数据
        $tr_planets = $this->transitsData[1]['planets'];// 行运-行运星数据
        $tp_planets = $this->thirdprogressedData['planets'];// 三限星体数据
        $tp_structure_t_square = $this->thirdprogressedData['structure']['t_square'];// 三限星体结构[T三角]数据

        $is_bad_5_half = true;

        foreach ([4, 6, 7, 8, 9] as $i)
        {
            if ($tp_planets[$i]['speed'] > config('astrolog.speed')[$i])
            {
                $is_bad_5_half = false;
            }
        }

        // 遍历宫位
        foreach ($house as $h)
        {
            // 初始化
            $bad_1 = 0;
            $bad_2 = [];
            $bad_3 = ['main' => 0, 'other' => 0];
            $bad_3_2 = 0;
            $bad_5 = 0;

            $bad_6 = 0;
            $bad_7 = 0;
            $bad_9 = ['dis' => 0, 'undis' => 0];
            $bad_10 = 0;
            $good_1 = 0;
            $good_2 = [0 => 0, 90 => 0, 'other' => 0];
            $good_3 = [0 => 0, 90 => 0, 'other' => 0];
            $good_5 = 0;
            $good_6 = [0 => 0, 90 => 0, 'other' => 0];
            $good_7 = [0 => 0, 90 => 0, 'other' => 0];
            $score['house'][$h] = ['good' => 0, 'bad' => 0];

            // 遍历凶星
            foreach ($disasters as $dis)
            {
                // 三限凶星入宫
                if ($tp_planets[$dis]['in_house_id'] == $h)
                {
                    // 三限凶星入宫后刑冲主星数量
                    $phase_90_180_num = 0;
                    // 遍历主星
                    foreach ($main as $i => $m)
                    {
                        // 命主只在行运中算做主星
                        if ($i == 2) continue;
                        $bad_2[$m] = 0;
                        // tp凶星入宫未刑冲主星 红分+0.5，每多一颗+0.2
                        if (!isset($tp_planets[$dis]['phase'][$dis . '-' . $m])
                            || !in_array($tp_planets[$dis]['phase'][$dis . '-' . $m]['phase_e'], [90, 180]))
                        {
                            $phase_90_180_num++;
                        } // tp凶星入宫后刑冲主星 红分+1（刑冲相同主星的，每多一颗+0.4，不同主星的，每多一颗+1）
                        elseif (isset($tp_planets[$dis]['phase'][$dis . '-' . $m])
                            && in_array($tp_planets[$dis]['phase'][$dis . '-' . $m]['phase_e'], [90, 180]))
                        {
                            $bad_2[$m] = 0;
                        }
                    }
                    // tp凶星入宫（但未刑冲主星）红分+0.5，每多一颗+0.2
                    if ($phase_90_180_num === 0)
                    {
                        $bad_1++;
                    }
                }
                if (!empty($tp_planets[$dis]['phase']))
                {
                    // 遍历凶星相位
                    foreach ($tp_planets[$dis]['phase'] as $tp_dis_phase)
                    {
                        if ($tp_planets[$tp_dis_phase['id']]['in_house_id'] == $h)
                        {
                            // tp宫内星被凶星刑冲
                            if (in_array($tp_dis_phase['phase_e'], [90, 180]))
                            {
                                // 若宫内星为主星，红分+1（每多一颗+0.4，构成T三角额外+1），
                                if ($dis == $main[0] || $dis == $main[1])
                                {
                                    $bad_3['main']++;
                                    if (is_in_t_square($tp_structure_t_square, $tp_dis_phase['id']))
                                    {
                                        $score['house'][$h]['bad'] += 1;
                                    }

                                } // 否则红分+0.5（每多一颗+0.2，构成T三角额外+0.6）
                                else
                                {
                                    $bad_3['other']++;
                                    if (is_in_t_square($tp_structure_t_square, $tp_dis_phase['id']))
                                    {
                                        $score['house'][$h]['bad'] += 0.6;
                                    }
                                }
                            }
                            //tp宫内主星与凶星合
                            //	红分+0.5（每多一颗凶星合+0.2）
                            elseif (in_array($tp_dis_phase['phase_e'], [0])
                                && ($dis == $main[0] || $dis == $main[1]))
                            {
                                $bad_3_2++;
                            }
                        }
                    }
                }
                // 行运凶星入宫（但未有刑冲合）
                // 红分+0.5，每多一颗+0.2
                if ($tr_planets[$dis]['in_house_id'] == $h)
                {
                    $tr_dis_phase_090180_num = 0;
                    if (!empty($tr_planets[$dis]['phase']))
                    {
                        foreach ($tr_planets[$dis]['phase'] as $tr_dis_phase)
                        {
                            // 合刑冲
                            if (in_array($tr_dis_phase['phase_e'], [0, 90, 180]))
                            {
                                $tr_dis_phase_090180_num++;
                            }
                            if (in_array($tr_dis_phase['phase_e'], [90, 180]))
                            {
                                // tr凶星入宫后克主星 刑冲+0.5（每多一颗+0.2）
                                if (in_array($tr_dis_phase['id'], $main))
                                {
                                    $bad_6++;
                                } // tr凶星入宫后刑冲非主星
                                else
                                {
                                    $bad_7++;
                                }
                            }
                        }
                    }
                    // tr凶星入宫（但未有刑冲合） 红分+0.5，每多一颗+0.2
                    if ($tr_dis_phase_090180_num === 0)
                    {
                        $bad_5++;
                    }
                }
            }
            // 遍历本命星体
            foreach ($na_planets as $nap)
            {
                if ($nap['in_house_id'] == $h && !empty($nap['phase']))
                {
                    foreach ($nap['phase'] as $na_phase)
                    {
                        // tr本命宫内凶星与外圈成相 每有一个凶星相位+0.5，非凶星相位+0.3
                        if (in_array($nap['id'], $disasters))
                        {
                            if (in_array($na_phase['id'], $disasters))
                            {
                                $bad_9['dis']++;
                            } elseif ($na_phase['id'] < ASC) $bad_9['undis']++;
                        }
                        // tr本命宫内主星与外圈凶星成相 刑冲+1
                        if (in_array($nap['id'], $main)
                            && in_array($na_phase['id'], $disasters)
                            && in_array($na_phase, [90, 180]))
                        {
                            $bad_10++;
                        }
                    }
                }
            }
            if ($bad_1 >= 1) $score['house'][$h]['bad'] += (0.5 + ($bad_1 - 1) * 0.2);
            if (!empty($bad_2)) foreach ($bad_2 as $k => $b2) if ($b2 >= 1) $score['house'][$h]['bad'] += (1 + ($b2 - 1) * 0.4);
            if ($bad_3['main'] > 0) $score['house'][$h]['bad'] += (1 + ($bad_3['main'] - 1) * 0.4);
            if ($bad_3['other'] > 0) $score['house'][$h]['bad'] += (0.5 + ($bad_3['other'] - 1) * 0.2);
            if ($bad_3_2 > 0) $score['house'][$h]['bad'] += (0.5 + ($bad_3_2 - 1) * 0.2);

            if ($bad_5 > 0)
            {
                //火土天海冥非缓滞状态，得分折半，否则为全部完整得分
                if ($is_bad_5_half !== false) $score['house'][$h]['bad'] += (0.5 + ($bad_5 - 1) * 0.2) / 2;
                else $score['house'][$h]['bad'] += (0.5 + ($bad_5 - 1) * 0.2);
            }

            if ($bad_6 > 0) $score['house'][$h]['bad'] += (1 + ($bad_6 - 1) * 0.5);
            if ($bad_7 > 0) $score['house'][$h]['bad'] += (0.5 + ($bad_7 - 1) * 0.2);

            $score['house'][$h]['bad'] += ($bad_9['dis'] * 0.5 + $bad_9['undis'] * 0.3);
            $score['house'][$h]['bad'] += $bad_10;


            // 遍历吉星
            foreach ($luck as $l)
            {
                // 吉星入宫
                if ($tp_planets[$l]['in_house_id'] == $h)
                {
                    foreach ($main as $i => $m)
                    {
                        // 命主只在行运中算做主星
                        if ($i == 2) continue;
                        // tp吉星入宫后连接主星
                        if (isset($tp_planets[$l]['phase'][$l . '-' . $m]))
                        {
                            if ($tp_planets[$l]['phase'][$l . '-' . $m]['phase_e'] == 0)
                            {
                                $good_2[0]++;
                            } elseif ($tp_planets[$l]['phase'][$l . '-' . $m]['phase_e'] == 90)
                            {
                                $good_2[90]++;
                            } else
                            {
                                $good_2['other']++;
                            }
//                            if($h==10)
//                            {
//                                print_r($good_2);
//                            }
                        } // tp吉星入宫（但未连接主星）
                        else
                        {
                            $good_1++;
                        }
                    }
                }
                if (!empty($tp_planets[$l]['phase']))
                {
                    foreach ($tp_planets[$l]['phase'] as $tp_phase)
                    {
                        // tp宫内星连接吉星
                        if ($tp_planets[$tp_phase['id']]['in_house_id'] == $h)
                        {
                            if ($tp_phase['phase_e'] == 0)
                            {
                                $good_3[0]++;
                            } elseif ($tp_phase['phase_e'] == 90)
                            {
                                $good_3[90]++;
                            } else
                            {
                                $good_3['other']++;
                            }
                        }
                    }
                }
                if (!empty($tr_planets[$l]['phase']))
                {
                    // tr本命宫内星连接外圈吉星
                    foreach ($tr_planets[$l]['phase'] as $tr_phase)
                    {
                        // tr宫内星连接吉星
                        if ($tp_planets[$tr_phase['id']]['in_house_id'] == $h)
                        {
                            if ($tr_phase['phase_e'] == 0)
                            {
                                $good_7[0]++;
                            } elseif ($tr_phase['phase_e'] == 90)
                            {
                                $good_7[90]++;
                            } else
                            {
                                $good_7['other']++;
                            }
                        }
                    }
                }
                if ($tr_planets[$l]['in_house_id'] == $h)
                {
                    if (empty($tr_planets[$l]['phase']))
                    {
                        $good_5++;
                    }
                    foreach ($main as $m)
                    {
                        // tr吉星入宫后连接主星
                        if (isset($tr_planets[$l]['phase'][$l . '-' . $m]))
                        {
                            if ($tr_planets[$l]['phase'][$l . '-' . $m]['phase_e'] == 0)
                            {
                                $good_6[0]++;
                            } elseif ($tr_planets[$l]['phase'][$l . '-' . $m]['phase_e'] == 90)
                            {
                                $good_6[90]++;
                            } else
                            {
                                $good_6['other']++;
                            }
                        }
                    }
                }
            }
            $score['house'][$h]['good'] += $good_1 * 0.5;
            if ($good_2[0] > 0) $score['house'][$h]['good'] += (1.2 + ($good_2[0] - 1) * 1.2 * 0.4);
//            echo " {$score['house'][$h]['good']}\n";
            if ($good_2[90] > 0) $score['house'][$h]['good'] += (0.75 + ($good_2[90] - 1) * 0.75 * 0.4);
            if ($good_2['other'] > 0) $score['house'][$h]['good'] += (1 + ($good_2['other'] - 1) * 0.4);
            if ($good_3[0] > 0) $score['house'][$h]['good'] += (1.2 + ($good_3[0] - 1) * 1.2 * 0.4);
            if ($good_3[90] > 0) $score['house'][$h]['good'] += (0.75 + ($good_3[90] - 1) * 0.75 * 0.4);
            if ($good_3['other'] > 0) $score['house'][$h]['good'] += (1 + ($good_3['other'] - 1) * 0.4);
            $score['house'][$h]['good'] += $good_5 * 0.5;
            if ($good_6[0] > 0) $score['house'][$h]['good'] += (1.2 + ($good_6[0] - 1) * 1.2 * 0.4);
            if ($good_6[90] > 0) $score['house'][$h]['good'] += (0.75 + ($good_6[90] - 1) * 0.75 * 0.4);
            if ($good_6['other'] > 0) $score['house'][$h]['good'] += (1 + ($good_6['other'] - 1) * 0.4);
            if ($good_7[0] > 0) $score['house'][$h]['good'] += (1.2 + ($good_7[0] - 1) * 1.2 * 0.4);
            if ($good_7[90] > 0) $score['house'][$h]['good'] += (0.75 + ($good_7[90] - 1) * 0.75 * 0.4);
            if ($good_7['other'] > 0) $score['house'][$h]['good'] += (1 + ($good_7['other'] - 1) * 0.4);
        }
        if (!empty($deputation))
        {
            // 遍历征象星
            foreach ($deputation as $dep)
            {
                $bad_4 = 0;
                $bad_8 = 0;
                $good_4 = [0 => 0, 90 => 0, 'other' => 0];
                $good_8 = [0 => 0, 90 => 0, 'other' => 0];
                $score['deputation'][$dep]['bad'] = 0;

                if ($tp_planets[$dep]['in_house_id'] == 12)
                {
                    $score['deputation'][$dep]['bad'] += 0.5;
                }

                if (!empty($tp_planets[$dep]['phase']))
                {
                    // 遍历三限征象星相位
                    foreach ($tp_planets[$dep]['phase'] as $tp_dep_phase)
                    {
                        // tp征象星被刑冲 红分+1，每多一颗+0.4，构成T三角额外+1
                        if (in_array($tp_dep_phase['phase_e'], [90, 180]))
                        {
                            $bad_4++;
                            // 构成T三角额外+1
                            if (is_in_t_square($tp_structure_t_square, $dep))
                            {
                                $score['deputation'][$dep]['bad'] += 1;
                            }
                        }
                    }
                }
                if (!empty($tr_planets[$dep]['phase']))
                {
                    // 遍历行运征象星相位
                    foreach ($tr_planets[$dep]['phase'] as $tr_dep_phase)
                    {
                        // tr征象星被凶星合刑冲  +1（每多一颗+0.5，在此前提下有凶星拱六分+0.3）
                        if (in_array($tr_dep_phase['phase_e'], [0, 90, 180]) && in_array($tr_dep_phase['id'], $disasters))
                        {
                            $bad_8++;
                            if (in_array($tr_dep_phase['phase_e'], [60, 120]) && in_array($tr_dep_phase['id'], $disasters))
                            {
                                $score['deputation'][$dep]['bad'] += 0.3;
                            }
                        }
                    }
                }
                if ($bad_4 > 0) $score['deputation'][$dep]['bad'] += (1 + ($bad_4 - 1) * 0.4);
                if ($bad_8 > 0) $score['deputation'][$dep]['bad'] += (1 + ($bad_8 - 1) * 0.5);

                $score['deputation'][$dep]['good'] = 0;
                // 遍历吉星
                foreach ($luck as $l)
                {
                    // tp征象星连接吉星
                    if (isset($tp_planets[$dep]['phase'][$dep . '-' . $l]))
                    {
                        if ($tp_planets[$dep]['phase'][$dep . '-' . $l]['phase_e'] == 0)
                        {
                            $good_4[0]++;
                        } elseif ($tp_planets[$dep]['phase'][$dep . '-' . $l]['phase_e'] == 90)
                        {
                            $good_4[90]++;
                        } else
                        {
                            $good_4['other']++;
                        }
                    }
                    // tr本命征象星与外圈吉星连接
                    if (!empty($tr_planets[$l]['phase']))
                    {
                        foreach ($tr_planets[$l]['phase'] as $tr_phase)
                        {
                            if ($tr_phase['id'] == $dep)
                            {
                                if ($tr_phase['phase_e'] == 0)
                                {
                                    $good_8[0]++;
                                } elseif ($tr_phase['phase_e'] == 90)
                                {
                                    $good_8[90]++;
                                } else
                                {
                                    $good_8['other']++;
                                }
                            }
                        }
                    }
                }
                if ($good_4[0] > 0) $score['deputation'][$dep]['good'] += (1.2 + ($good_4[0] - 1) * 1.2 * 0.4);
                if ($good_4[90] > 0) $score['deputation'][$dep]['good'] += (0.75 + ($good_4[90] - 1) * 0.75 * 0.4);
                if ($good_4['other'] > 0) $score['deputation'][$dep]['good'] += (1 + ($good_4['other'] - 1) * 0.4);

                if ($good_8[0] > 0) $score['deputation'][$dep]['good'] += (1.2 + ($good_8[0] - 1) * 1.2 * 0.4);
                if ($good_8[90] > 0) $score['deputation'][$dep]['good'] += (0.75 + ($good_8[90] - 1) * 0.75 * 0.4);
                if ($good_8['other'] > 0) $score['deputation'][$dep]['good'] += (1 + ($good_8['other'] - 1) * 0.4);
            }
        }

        // 吉凶值返回
        $result = [];

        foreach ($score as $k => $s)
        {
            foreach ($s as $sh)
            {
                if ($k == 'house')
                {
                    if (!isset($result['house']['good'])) $result['house']['good'] = 0;
                    if (!isset($result['house']['bad'])) $result['house']['bad'] = 0;
                    $result['house']['good'] += $sh['good'];
                    $result['house']['bad'] += $sh['bad'];
                } else
                {
                    if (!isset($result['planet']['good'])) $result['planet']['good'] = 0;
                    if (!isset($result['planet']['bad'])) $result['planet']['bad'] = 0;
                    $result['planet']['good'] += $sh['good'];
                    $result['planet']['bad'] += $sh['bad'];
                }
            }
        }

        return $result;
    }
}
